package ${pkg}.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kool.core.exception.AppException;
import ${pkg}.bean.${upTumTabNam}Bean;
import com.kool.system.bean.io.SYPAGEINFOYBean;
import com.kool.system.bean.vo.PageResult;
import ${pkg}.dao.${upTumTabNam}Dao;

/**
 * @AUTHOR LUYU
 */
@Service
public class ${businessName}Service {
	private static Logger logger = Logger.getLogger(${businessName}Service.class);

	@Autowired
	private ${upTumTabNam}Dao ${lowTumTabNam}Dao;

	/**
	 * @DESCRIBE 增加
	 */
	public void add(${upTumTabNam}Bean ${lowTumTabNam}) throws AppException {
		if (null == ${lowTumTabNam}) {
			throw new AppException("SY000027");// 输入信息为空
		}
		logger.debug(${lowTumTabNam}.toString());
		${lowTumTabNam}Dao.insert(${lowTumTabNam});
	}

	/**
	 * @DESCRIBE 修改
	 */
	public void update(${upTumTabNam}Bean ${lowTumTabNam}) throws AppException {
		if (null == ${lowTumTabNam}) {
			throw new AppException("SY000027");// 输入信息为空
		}
		logger.debug(${lowTumTabNam}.toString());
		${lowTumTabNam}Dao.update(${lowTumTabNam});
	}

	/**
	 * @DESCRIBE 删除
	 */
	public void delete(${upTumTabNam}Bean ${lowTumTabNam}) throws AppException {
		if (null == ${lowTumTabNam}) {
			throw new AppException("SY000027");// 输入信息为空
		}
		logger.debug(${lowTumTabNam}.toString());
		${lowTumTabNam}Dao.delete(${lowTumTabNam});
	}

	/**
	 * @DESCRIBE 分页查询
	 */
	public PageResult queryPage(${upTumTabNam}Bean ${lowTumTabNam}, SYPAGEINFOYBean pageInfo) {
		logger.debug(${lowTumTabNam}.toString());
		int total = ${lowTumTabNam}Dao.selectTotal(${lowTumTabNam});

		int currentPage = pageInfo.getCurrentPage();
		int pageSize = pageInfo.getPageSize();
		int offset = (currentPage - 1) * pageSize;
		int pageCount = (total + pageSize - 1) / pageSize;
		List<${upTumTabNam}Bean> list = ${lowTumTabNam}Dao.selectPage(${lowTumTabNam}, offset, pageInfo.getPageSize());

		pageInfo.setTotal(total);
		pageInfo.setPageCount(pageCount);

		PageResult pageResult = new PageResult();
		pageResult.setListBusinessValue(new ArrayList<>(list));
		pageResult.setPageInfo(pageInfo);
		return pageResult;
	}

	/**
	 * @DESCRIBE 查询
	 */
	public List<${upTumTabNam}Bean> query(${upTumTabNam}Bean ${lowTumTabNam}) throws AppException {
		if (null == ${lowTumTabNam}) {
			${lowTumTabNam} = new ${upTumTabNam}Bean();
		}

		logger.debug(${lowTumTabNam}.toString());
		return ${lowTumTabNam}Dao.select(${lowTumTabNam});
	}
}
